// pages/shop-details/details.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    vertical: false,
    autoplay: false,
    interval: 3000,
    duration: 500,
    circular: true,
    id:'',
    details:'',
    comment:'',
    publishshow: false,
    focus: false,
    value: '',
  },
  // 详情
  getdetails: function () {
    ajax('/api/index/good_show', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        id:this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      var arr = res.data
      var li1 = '.jpg', li2 = '.jpeg', li3 = '.png', li4 = '.gif';
      arr.plan_arrs = []
      if (arr.plan_arr) {
        for (var a in arr.plan_arr) {
          if (arr.plan_arr[a].includes(li1) || arr.plan_arr[a].includes(li2) || arr.plan_arr[a].includes(li3) || arr.plan_arr[a].includes(li4)) {
            arr.plan_arrs[a] = { 'url': arr.plan_arr[a], 'style': '0' }
          } else {
            arr.plan_arrs[a] = { 'url': arr.plan_arr[a], 'style': '1' }
          }
        }
      }
      this.setData({
        details: res.data
      })
    })
  },
  // 评论记录
  getcomment: function () {
    ajax('/api/index/forum_show_comment', {
      data: {
        type: 3,
        id: this.data.id,
        page: 1,
        limit: 1
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      this.setData({
        comment: res.data
      })
    })
  },
  // 收藏 
  shoucang: function () {
    ajax('/api/user/favorite_main', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "put",
    }, res => {
      this.getdetails()
    })
  },
  // 查看更多评论
  seemore:function(e){
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/comment-list/list?id=' + id,
    })
  },
  // 一级评论
  pinglun: function () {
    this.setData({
      publishshow: true
    });
  },
  // 获取输入框内容
  inputvalue: function (e) {
    this.setData({
      value: e.detail.value,
      focus: true,
    });
  },
  // 发表1级
  fabiaos: function () {
    ajax('/api/user/comment_first_add', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        type: 3,
        zid: this.data.id,
        content: this.data.value
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      this.getcomment()
      this.setData({
        publishshow: false,
        focus: false,
        value: '',
      })
    })
  },
  // 立即抢购
  qianggou: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/sure-order/order?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    this.getdetails()
    this.getcomment()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})